package com.diyunapp.xilinapp.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.diyunapp.xilinapp.utils.CodeCountDown;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResgiterFirActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.user_phone_et)
    EditText userPhoneEt;
    @Bind(R.id.phone_code_et)
    EditText phoneCodeEt;
    @Bind(R.id.getCode_tv)
    TextView getCodeTv;
    @Bind(R.id.login_tv)
    TextView loginTv;
public String code;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resgiter_fir);
        ButterKnife.bind(this);
        View headView = findViewById(R.id.register_menubar_inc);
        super.MenuWidget(headView);
    }

    @OnClick({R.id.go_back, R.id.getCode_tv, R.id.login_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                finish();
                break;
            case R.id.getCode_tv:
                if(!TextUtils.isEmpty(userPhoneEt.getText().toString().trim())){
                    initCode();
                }

                break;
            case R.id.login_tv:
                if(!TextUtils.isEmpty(phoneCodeEt.getText().toString().trim())){
                    if(code==phoneCodeEt.getText().toString().trim()||code.equals(phoneCodeEt.getText().toString().trim())){
                        Intent intent = new Intent();
                        intent.putExtra("phoneNum",userPhoneEt.getText().toString().trim());
                        intent.setClass(this, RegisterActivity.class);
                        startActivityForResult(intent,1);
                    }else {
                        toast("您输入的验证码不正确");
                    }
                }
                break;
        }
    }

    private void initCode() {
        String url= MainActivity.url+"act=goods&op=reg_sendsms&mobile="+userPhoneEt.getText().toString().trim();
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo1=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            if(result==0){
                                String data=resultObject.optString("data");
                                toast(data);
                            }
                            if(result==200){
                                getCodeTv.setClickable(false);
//                                getCodeTv.setTextColor(unpressColor);
                                CodeCountDown.with(ResgiterFirActivity.this).into(getCodeTv);
                                String data=resultObject.optString("data");
                                code=data;
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
//                            toast("查询失败");
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");
                    }
                });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == 1) {
                finish();

            }
        }
        super.onActivityResult(requestCode, resultCode, data);}
    }